<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<style>
    .addbut{position: absolute;right:9px;top: 9px;height: 34px;line-height: 34px!important;width:70px;padding:0 3px;background-color: #1ca5ff;}
    .guanbut{position: absolute;right:9px;top: 9px;height: 34px;line-height: 34px!important;width:37px;padding:0 3px;background-color: red!important;color: #FFFFFF}
</style>
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con">
            <span>新建人员证书</span><div class="move_title"></div><?php include "../public/sub.php";?></div>
    </div>
    <div class="main-container">

        <div class="block-generic">


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>人员姓名</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="personid" class="layui-input personid" placeholder="请选择" onclick="openPage('../public/choose_person.php?dom=personid&page=cert','80%','80%')">
                            <div class="choose_div" id="personid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_person.php?dom=personid&page=cert','80%','80%')">
                                <i class="layui-icon layui-icon-search"></i>
                            </span>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>证书分类</label>
                        <div class="layui-input-block">
                            <div id="type"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>证书名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" id="name" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">证书编号</label>
                        <div class="layui-input-block">
                            <input type="text" name="number" id="number" placeholder="请输入" clearable  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">发证单位</label>
                        <div class="layui-input-block">
                            <input type="text" name="issue_unit" id="issue_unit" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">注册/发证日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="issue_dt" id="issue_dt" placeholder="请选择" clearable autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">专业</label>
                        <div class="layui-input-block">
                            <input type="text" placeholder="请输入" clearable autocomplete="off" name="major[]" class="layui-input major">
                            <button type="button" class="layui-btn addbut add_zeng" onclick="add_row()">
                                <i class="layui-icon layui-icon-addition"></i>增项
                            </button>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>!</text>有效期止</label>
                        <div class="layui-input-block">
                            <input type="text" placeholder="请选择" clearable autocomplete="off" name="expiry_dt[]" class="layui-input expiry_dt bdate_ymd">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item" id="zeng">
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>证书形式</label>
                        <div class="layui-input-block">
                            <select class="layui-select form" id="form" name="form" lay-filter="form"></select>
                            <p class="error" id="form_error" style="margin: 0;"></p>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>证书状态</label>
                        <div class="layui-input-block">
                            <select class="layui-select " name="state" id="state" lay-filter="state"></select>
                            <p class="error" id="state_error" style="margin: 0;"></p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>!</text>电子件有效期</label>
                        <div class="layui-input-block">
                            <input type="text" name="electron_dt" id="electron_dt" placeholder="请选择" clearable autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>!</text>下次年检时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="inspect_dt" id="inspect_dt" placeholder="请选择" clearable autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="outDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">注销/转出时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="out_dt" id="out_dt" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">保管人</label>
                        <div class="layui-input-block">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=custodianid&type=use','770px','490px');"></div>
                                <div class="tanxiannei" id="custodianid"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=custodianid&type=use','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                            <div id="custodianid_tanerror" class="tanerrormsg"></div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">存放地</label>
                        <div class="layui-input-block">
                            <div id="store_addr"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>

            <?php include '../public/upinfo.php'?>
        </div>


    </div>
</form>
<script>
    var personid = getQueryVariable('personid') || 0;

    $("#commentForm").validate({
        rules: {
            name: {required: true},
        },
        messages:{
            name: {required: "请输入"},
        }
    });



    function remove_row(obj){
        $(obj).parent().parent().remove();

        $('#zeng>div').each(function (index) {
            $(this).find('.majortit').text('增项专业'+(index+1));
            $(this).find('.dttit').text('增项有效期'+(index+1));
        })
    }
    function add_row(){
        var index = $('#zeng>div').length;

        var str = '';
        str += '<div class="layui-row layui-col-space10">';
        str += '<div class="layui-col-md6">';
        str += '<label class="layui-form-label majortit">增项专业'+(index+1)+'</label>';
        str += '<div class="layui-input-block" style="position: relative;">';
        str += '<input type="text" name="major[]" clearable class="layui-input">';
        str += '</div>';
        str += '</div>';
        str += '<div class="layui-col-md6">';
        str += '<label class="layui-form-label dttit">增项有效期'+(index+1)+'</label>';
        str += '<div class="layui-input-block" style="margin-left: 0 !important;width: calc(100% - 156px) !important;float: left;">';
        str += '<input type="text" placeholder="请选择" clearable autocomplete="off" name="expiry_dt[]" class="layui-input bdate_ymd">';
        str += '</div>';
        str += '<button type="button" class="layui-btn guanbut" onclick="remove_row(this)"><i class="layui-icon layui-icon-close"></i></button>';
        str += '</div>';
        str += '</div>';
        $('#zeng').append(str);
        render_date();
    }



    layui.use(['laytp','form'], function() {
        var form = layui.form;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_cert_add, data:{personid:personid}}).done(function(res){
            if (res.code === 2000) {
                if(res.data.personinfo){
                    set_choose_btn('personid',res.data.personinfo.id,res.data.personinfo.name);
                }

                var custodian_info = res.data.data.custodian_info;
                if(custodian_info){
                    tan_tags_init('#custodianid', custodian_info);
                }



                var form = res.data.form;
                var form_str = '<option value=""></option>';
                for (var i in form){
                    form_str += '<option value="'+i+'">'+form[i]+'</option>';
                }
                $('#form').html(form_str);

                var state = res.data.state;
                var state_str = '';
                for (var i in state){
                    state_str += '<option value="'+i+'">'+state[i]+'</option>';
                }
                $('#state').html(state_str);

                xmselect_dan('type', res.data.type, '', function (res) {
                    if(res.arr && res.arr.length > 0){
                        $("#type").find('xm-select').removeClass('error');
                    }else{
                        $("#type").find('xm-select').addClass('error');
                    }
                });
                xmselect_dan('store_addr', res.data.store_addr, '');

                public_upload_file_load('upload_files');
                render_from();
            }
        });


        form.on('select(state)', function(data){
            if(data.value == ''){
                $('#state_error').show().text('请选择');
                $('#state_error').parent().find('input').addClass('select_error');
            }else{
                $('#state_error').hide();
                $('#state_error').parent().find('input').removeClass('select_error');
            }
            if(data.value == 2 || data.value == 3){
                $('#outDiv').removeClass('layui-hide');
            }else{
                $('#outDiv').addClass('layui-hide');
            }
        });
        form.on('select(form)', function(data){
            if(data.value == ''){
                $('#form_error').show().text('请选择');
                $('#form_error').parent().find('input').addClass('select_error');
            }else{
                $('#form_error').hide();
                $('#form_error').parent().find('input').removeClass('select_error');
            }
        });

        form.on('submit(add)', function(data){
            var validRes = $("#commentForm").valid();
            data.field.custodianid = tan_tags_getvalue('#custodianid', 'user');
            data.field.personid = get_choose_val('personid');


            if(data.field.form == ''){
                $('#form_error').show().text('请选择');
                $('#form_error').parent().find('input').addClass('select_error');
                validRes = false;
            }else{
                $('#form_error').hide();
                $('#form_error').parent().find('input').removeClass('select_error');
            }
            if(data.field.state == ''){
                $('#state_error').show().text('请选择');
                $('#state_error').parent().find('input').addClass('select_error');
                validRes = false;
            }else{
                $('#state_error').hide();
                $('#state_error').parent().find('input').removeClass('select_error');
            }

            formValid('personid');
            if(data.field.personid == undefined || !data.field.personid){
                validRes = false;
            }


            if(!data.field.type){
                $("#type").find('xm-select').addClass('error');
                validRes = false;
            }else{
                $("#type").find('xm-select').removeClass('error');
            }

            if(!validRes){return false;}


            data.field.files = public_upload_file_value('upload_files');
            facade.ajax({url: ask_cert_add, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });

    });
</script>
</body>
</html>
